﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace KetBanBonPhuong
{
    /// <summary>
    /// Summary description for Admin
    /// </summary>
    public class Admin
    {
        private int admin_id;

        public int Admin_id
        {
            get { return admin_id; }
            set { admin_id = value; }
        }

        private string email;

        public string Email
        {
            get { return email; }
            set { email = value; }
        }

        private string password;

        public string Password
        {
            get { return password; }
            set { password = value; }
        }

        public Admin()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public static DataTable GetAll()
        {
            DBHelp dbh = new DBHelp();
            DataTable dt = new DataTable();
            string cmd = "Admin_GetAll";
            dt = dbh.ExecuteDataTable(cmd, CommandType.StoredProcedure);
            return dt;
        }

        public static Admin GetInfo(int ID)
        {
            DBHelp dbh = new DBHelp();
            dbh.AddParameter("@admin_id", ID);
            Admin admin = new Admin();
            string cmd = "select * from tbl_admin where admin_id = @admin_id";
            SqlDataReader sdr = dbh.ExecuteReader(cmd, CommandType.Text);
            if (sdr.Read())
            {
                admin.Admin_id = sdr.GetInt32(0);
                if (!sdr.IsDBNull(1))
                    admin.Email = sdr.GetString(1);
                if (!sdr.IsDBNull(2))
                    admin.password = sdr.GetString(2);
            }
            return admin;
        }

        public static void Insert(Admin admin)
        {
            DBHelp dbh = new DBHelp();
            dbh.AddParameter("@email", admin.Email);
            dbh.AddParameter("@password", admin.Password);
            string cmd = "Admin_Insert";
            dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }

        public static void Update(Admin admin)
        {
            DBHelp dbh = new DBHelp();
            dbh.AddParameter("@admin_id", admin.Admin_id);
            dbh.AddParameter("@email", admin.Email);
            dbh.AddParameter("@password", admin.Password);
            string cmd = "Admin_Update";
            dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }

        public static void Delete(int ID)
        {
            DBHelp dbh = new DBHelp();
            dbh.AddParameter("@admin_id", ID);
            string cmd = "Admin_Delete";
            dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }

        public static int AdminLogin(string Email, string Password)
        {
            // viet ham admin login
            DBHelp dbh = new DBHelp();
            dbh.AddParameter("@email", Email);
            DataTable dt = new DataTable();
            string cmd = "select * from tbl_admin where email = @email";
            dt = dbh.ExecuteDataTable(cmd, CommandType.Text);
            if (dt.Rows.Count <= 0)
            {
                return -1; // sai email
            }
            foreach (DataRow row in dt.Rows)
            {
                if (Password == row["password"].ToString())
                {
                    return Convert.ToInt32(row["admin_id"].ToString()); // login successfully
                }
            }
            return 0; // sai password
        }
    }
}